package com.maycurobj.service.impl;

import com.maycurobj.repository.bim.TbtaccRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 统一中台的接口服务
 */
@Service
@Slf4j
public class BimService {

    @Autowired
    private TbtaccRepository tbtaccRepository;

    /**
     * 获取一个用户钉钉userid
     * @param guid
     * @return
     */
    public String getDingUserID(String guid){
        String userGuid = tbtaccRepository.getDingUserID(guid);
        return userGuid;
    }


    /**
     * 通过crm 工号 获取钉钉userid
     * @param crmUserid
     * @return
     */
    public String getDingUserIdByCrmworkcode(String crmUserid) {
        String userid= tbtaccRepository.getDingUserIdByCrmworkcode(crmUserid);
        return userid;
    }

    public String getIamUsernameByDingId(String dingUserid) {
        String username = tbtaccRepository.getIamUsernameByDingId(dingUserid);
        return username;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteIamTodoByAozhe(){
       Integer count= tbtaccRepository.deleteIamTodoByAozhe();
        log.info("完成删除{}记录", count);
    }

    /**
     * 通过crm 工号组 获取钉钉userid组（全部用，号隔开）
     * @param crmUserids
     * @return
     */
    public String getDingUserIdByCrmworkcodes(String crmUserids){
        try {
            String[] args = crmUserids.split(",");
            String returnStr="";
            for (int i = 0; i < args.length; i++) {
                String _s=getDingUserIdByCrmworkcode(args[i]);
                if(StringUtils.isBlank(_s)){
                    continue;
                }
                returnStr+=_s;

                if(i+1!=args.length){
                    returnStr+=",";
                }
            }
            return returnStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
